package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.ICommentMapper;
import com.tedu.weibo.pojo.dto.CommentDTO;
import com.tedu.weibo.pojo.entity.Comment;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.vo.CommentVO;
import com.tedu.weibo.result.JsonResult;
import com.tedu.weibo.result.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

//@Controller
@RestController
@RequestMapping("/v1/comment")
@Api(tags = "评论模块")
public class CommentController {

    @Autowired
    private ICommentMapper iCommentMapper;

    @ApiOperation("发表评论")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user==null){
            return new JsonResult(StatusCode.NOT_LOGIN);//未登录，评论失败
        }
        Comment comment = new Comment();
        comment.setUserId(user.getId());
        comment.setCreated(new Date());
        BeanUtils.copyProperties(commentDTO,comment);
        iCommentMapper.insert(comment);
        return JsonResult.success();//评论成功
    }

    @ApiOperation("获取指定微博的所有评论")
    @GetMapping("/selectByWeiboId")
    public JsonResult selectByWeiboId(Integer id){
        return JsonResult.success(iCommentMapper.selectByWeiboId(id)) ;
    }
}
